package com.tos.second;


import com.tos.second.utils.MailMsg;
import io.lettuce.core.RedisConnectionException;
import jakarta.mail.MessagingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @作者：张德明
 * @时间：2024/12/17 下午9:54
 */

@Component("initRun")
public class InitRun implements ApplicationRunner {

    private static final Logger log = LoggerFactory.getLogger(InitRun.class);

    @Autowired
    private DataSource dataSource;

    @Autowired
    private MailMsg mailMsg;

    @Autowired
    private RedisTemplate redisTemplate;



    @Override
    public void run(ApplicationArguments args) throws Exception {
        try {
            dataSource.getConnection();
            redisTemplate.opsForValue().get("test");
            log.info("服务启动成功，可以开始快乐的编程啦");
        }catch (SQLException e) {
            log.error("数据库配置错误，请检查mysql配置");
        }catch (RedisConnectionFailureException e){
            log.error("redis配置错误，请检查redis配置");
            try {
                mailMsg.mailException("885748445@qq.com","redis操作失败，请管理员及时检查");
            } catch (MessagingException ex) {
                log.error("发送失败");
            }
        } catch (Exception e) {
            log.error("服务启动失败。");
        }
    }
}
